package utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import play.Logger;
import sun.misc.BASE64Encoder;

public class UrlUtil {

    /**
     * 生成签名
     * 签名方式：sid+token+timestamp
     */
    public static String generateSignature(String sid,String timeStamp,String token){
        EncryptUtil util = new EncryptUtil();
        String result = null;
        try {
            result = util.md5Digest(sid+timeStamp+token).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 生成时间戳
     */
    public static String  generateTimestamp(){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        return sdf.format(now);
    }
    
    /**
     * 帐号申请的时间戳格式
     * @return
     */
    public static String generateSignTimestamp(){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_PATTERN);
        return sdf.format(now);
    }
    
    /**
     * 生成帐号申请的URL的sigParameter信息
     * @param sid
     * @param token
     * @param timeStamp
     * @return
     */
    public static String generateSigParameter(String sid,String token,String timeStamp){
        EncryptUtil util = new EncryptUtil();
        String result = null;
        try {
            result = util.md5Digest(sid+token+timeStamp).toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 生成Authorization信息
     * @param sid
     * @param timestamp
     * @return
     */
    public static String generateAuthorization(String sid,String timestamp){
        String str = sid+":"+timestamp;
        EncryptUtil util = new EncryptUtil();
        String result = null;
        try {
            result = util.base64Encoder(str);
        } catch (Exception e) {
            Logger.error(e,"generateAuthorization failed!");
        }
        return result;
       
    }
   
    
    
    
    
    
    private final static String DEFAULT_PATTERN="yyyyMMddHHmmssSSS";
    
    private final static String SHORT_PATTERN="yyyyMMddHHmmss";
    


   
    
    
    
    
    
    
    
   
}
